
package com.scalar.chiptrack.alarms;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.scalar.chiptrack.alarms.dao.AlarmDAO;


public final class AlarmTest {

    public AlarmTest() {
    }

    public static void main (String[] args) {

        try {
            
            // CREATE ORIGINATOR AND RECIPIENTS
            ArrayList recipients = new ArrayList();
            AlarmUser from = new AlarmUser ("LokeshN", "chiptrac@scalarsoft.com");
            AlarmUser to = new AlarmUser ("LokeshN", "chiptrac2@scalarsoft.com");
            recipients.add (to);

            // CREATE EXPIRE DATE
            Calendar cal = Calendar.getInstance();
            cal.set (Calendar.MONTH, 6);
            cal.set (Calendar.DAY_OF_MONTH, 15);
            Date expiresOn = cal.getTime();

            // -------------------------- CREATE ALARM -------------------------------
            
            Alarm newAlarm = new Alarm ("Test Alarm", "TEST_ALARM", from);
            newAlarm.setExpiresOn(expiresOn.getTime());
            newAlarm.setRecipients (recipients);
            newAlarm.setMessage ("This is a test alarm with \"_NORMAL_\" priority");
            
            // ACTIVATE
            //newAlarm.activate(null /*DBTransaction*/);
            System.out.println ("Alarm \"" + newAlarm.getName() + "\" has been activated");
            
            // NOTIFY EVERYBODY
            //newAlarm.notifyAllRecipients();
            //System.out.println ("Notified all recipients");
            
            // ADD COMMENTS
            //Comment comment = new Comment (to.getName(), "This sales order should be approved ASAP");
            //newAlarm.addComment (null /*DBTransaction*/, comment);
            //System.out.println ("Comment has been added");
            

            
            // ---------------------- LOAD EXISTING ALARM AND MODIFY -----------------
            
            int alarmId = 66;   //newAlarm.getId();
            
            System.out.println ("\n\nLoading Alarm for AlarmID = " + alarmId);
            Alarm oldAlarm = AlarmDAO.loadAlarmById (alarmId);
            
            System.out.println (oldAlarm);

            // SET USER RESPONSE
            System.out.println (to.getName() + " RESPONDED -------------");
            oldAlarm.setUserResponse (to.getName(), Alarm.ALARM_RESPONSE_ACCEPT, "This order has been approved");

            // LOAD THE MODIFIED ALARM AGAIN FROM DATABASE
            oldAlarm = AlarmDAO.loadAlarmById (alarmId);
            System.out.println ("\n\nMODIFIED Alarm STATE -\n" + oldAlarm);
            
            
            // ------------------------ CLOSE THE ALARM ------------------------------
            oldAlarm.close();
            System.out.println ("Alarm (ID = " + alarmId + ") has been closed");
            
            // NOTIFY EVERYBODY ABOUT IT
            oldAlarm.notifyAllRecipients();
            System.out.println ("Notified all recipients");
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}